<?php
/**
 * fields Type & description deprecated
 * position & slug to add
 	ALTER TABLE `categories` ADD `slug` VARCHAR( 40 ) NOT NULL AFTER `title` ,
	ADD `position` INT( 10 ) NULL DEFAULT '1' AFTER `slug` ,
	ADD `pages_count` INT( 10 ) NOT NULL DEFAULT '0' AFTER `position` ;
 **/
 
 /**
  * TODO :
  * define error types in data array
  * */
 
 
 class Category extends AppModel
 {
 	var $name = 'Category';
 	
 	var $validate = array(
 		/*'type' => VALID_NOT_EMPTY,*/ // deprecated
		'title' => VALID_NOT_EMPTY,
		'slug' => VALID_NOT_EMPTY, //must be unique
		'position' => VALID_NUMBER
 		);
 		
 	var $hasMany = array(
			'Page' => array(
 				'className' => 'Page',
 				'conditions' => '',
 				'order' => '',
 				'foreignKey' => 'category_id')
 		);
 		
 	function select_categories(){
		$select_categories = array();
		$categories = $this->findAll();
		foreach($categories as $category){
			$select_categories += array($category['Category']['id'] => $category['Category']['title']);
		}
		return $select_categories;
	}
	
	function validates(){
 		$this->__test_slug();
 		return count($this->invalidFields()) == 0;
	}
 	
 	function __test_slug(){
		$slugs = $this->findBySlug($this->data['Category']['slug']);
		
		//edit page should test slug only on change
		if (!empty($slugs) && $slugs['Category']['id'] != $this->id){
			//slug already taken
			$this->invalidate('slug', 'Slug already used');
		}
	}
 }
?>
